<template>
  <div class="app">
    <el-alert v-show="showNotice"
      title="更新成功"
      type="success"
      show-icon style="margin-bottom:20px;">
    </el-alert>
    <el-form ref="form" :model="info" label-width="120px">
      <el-form-item label="用户名">
        <el-input v-model="info.username"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名">
         <el-input v-model="info.name"></el-input>
      </el-form-item>
      <el-form-item label="头像地址">
        <el-input v-model="info.avatar"></el-input>
      </el-form-item>
      <el-form-item label="用户类型">
        <el-select v-model="info.role">
          <el-option label="普通管理员" value="1">普通管理员</el-option>
          <el-option label="超级管理员" value="99">超级管理员</el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" v-on:click="opEdit">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getInfo, edit } from '@/api/admin'
export default {
  name: 'AdminEditor',
  data: function() {
    // 属性
    return {
      showNotice: false,
      info: {
        username: "",
        name: "",
        avatar: "",
        role: "1"
      }
    }
  },
  props: [
    'id'
  ],
  watch: {
    id: function(val, oldVal) {
      console.log('load user info ...')
      if (val > 0) {
        this.loadUserInfo(val)
      }
    }
  },
  created: function() {
    console.log('id:'+this.id)
    if (this.id > 0) {
      this.loadUserInfo(this.id)
    }
  },
  methods: {
    /** 组件使用的方法 */
    loadUserInfo(id) {
      // 载入用户信息
      getInfo(id).then(res => {
        this.info = res
      })
    },
    // 更新
    opEdit() {
      const param = {
        username: this.info.username,
        name: this.info.name,
        role: this.info.role,
        avatar: this.info.avatar,
        target_id: this.id
      }
      edit(param).then(response => {
          this.showNotice = true
      })
    }
  }
}
</script>

